package com.itboye.mengwu.app.ui.mymeng.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.ShoPingAdapter;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianYouKe;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianZengSongFang;
import com.itboye.mengwu.app.ui.mymeng.MyTieZiActivity;
import com.itboye.mengwu.data.model.ShopPingJiabean;
import com.itboye.mengwu.data.model.WodeZengSongShangPingBean;
import com.itboye.mengwu.data.presenter.FenSiPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.widget.BySqlUtil;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class MyMengMeZengSongFragment extends BaseFragment implements Observer,
		IXListViewListener {

	XListView myzengosng_ListView;
	FenSiPresenter presenter;
	WodeZengSongShangPingBean bean, beanTwo;
	String uid;
	MemengZengSongAdapter adapter;
	private int pageNo = 1;
	String id;
	String pid;
	boolean ShangLaflag = false;
	MemengZengSongAdapter memengZengSongAdapter;
	RelativeLayout nodata;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_mymengme_zengsong;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub

		myzengosng_ListView.setXListViewListener(this);
		myzengosng_ListView.setPullLoadEnable(true);
		myzengosng_ListView.setPullRefreshEnable(true);
		showProgressDialog("数据加载中，请稍后", true);
		presenter = new FenSiPresenter(this);
		bean = new WodeZengSongShangPingBean();
		uid = SPUtils.get(getActivity(), null, SpPublicParams.UID, "") + "";
		presenter.ShopZengSong(uid, pageNo + "", "10");
		adapter = new MemengZengSongAdapter(getActivity(), bean);
		bean.setId(id);
		bean.setPid(pid);
		pid = bean.getPid();
		id = bean.getId();
		myzengosng_ListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						ActivityZengWuXianZengSongFang.class);
				// intent.putExtra("pid",pid);
				intent.putExtra("pid", bean.getList().get(position - 1).getId());
				startActivity(intent);

			}
		});

	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == FenSiPresenter.ShopSuccess) {

				// adapter=new MemengZengSongAdapter(getActivity(), bean);
				beanTwo = new WodeZengSongShangPingBean();
				beanTwo = (WodeZengSongShangPingBean) entity.getData();
				if (ShangLaflag == true) {
					bean.getList().addAll(beanTwo.getList());
					if (bean.getList().size() == 0) {
						nodata.setVisibility(View.VISIBLE);
						myzengosng_ListView.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.GONE);
						myzengosng_ListView.setVisibility(View.VISIBLE);
					}
					memengZengSongAdapter.upDate(getActivity(), bean);
					memengZengSongAdapter.notifyDataSetChanged();
				} else {
					memengZengSongAdapter = new MemengZengSongAdapter(
							getActivity(), beanTwo);
					myzengosng_ListView.setAdapter(memengZengSongAdapter);
					bean = beanTwo;
				}

			}
			try {
				closeProgressDialog();
			} catch (Exception e) {
				// TODO: handle exception

			}
			stopLoad();
		}
	}

	class MemengZengSongAdapter extends BaseAdapter {

		Context context;
		WodeZengSongShangPingBean list;

		public MemengZengSongAdapter(Context context,
				WodeZengSongShangPingBean list) {
			this.context = context;
			this.list = list;
		}

		public void upDate(Context context, WodeZengSongShangPingBean list) {
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.getList() == null ? 0 : list.getList().size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HouderView houderView;
			if (convertView == null) {
				houderView = new HouderView();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_mymeng_mezengsong, null);
				houderView.txtShopTitle = (TextView) convertView
						.findViewById(R.id.txtShopTitle);
				houderView.txtZengTime = (TextView) convertView
						.findViewById(R.id.txtZengTime);

				houderView.txtMathtoZeng = (TextView) convertView
						.findViewById(R.id.txtMathtoZeng);

				houderView.txtmathCountext = (TextView) convertView
						.findViewById(R.id.txtmathCountext);
				houderView.txtCIty = (TextView) convertView
						.findViewById(R.id.txtCIty);
				houderView.txtregion = (TextView) convertView
						.findViewById(R.id.txtregion);
				houderView.linZengSong = (LinearLayout) convertView
						.findViewById(R.id.linZengSong);
				houderView.imgRoundImageview = (ImageView) convertView
						.findViewById(R.id.imgRoundImageview);
				convertView.setTag(houderView);
			} else {
				houderView = (HouderView) convertView.getTag();
			}
			houderView.txtShopTitle.setText(list.getList().get(position)
					.getNickname());

			houderView.txtZengTime.setText(new SimpleDateFormat("yyyy-MM-dd")
					.format(Long.parseLong(list.getList().get(position)
							.getUpdatetime()) * 1000));

			houderView.txtMathtoZeng.setText("妈妈赠");

			houderView.txtmathCountext.setText(list.getList().get(position)
					.getDetail());

			BySqlUtil sqlUtil = new BySqlUtil();
			// 打开数据库
			sqlUtil.openDataBase(context);
			// 根据邮编查询数据库得到地区地址
			sqlUtil.getProviceName(list.getList().get(position)
					.getLocProvince(), list.getList().get(position)
					.getLocCity(), context);

			houderView.txtregion.setText(sqlUtil.getProvinceName()
					+ sqlUtil.getCityName());

			if (list.getList().get(position).getSendOut().equals("0")) {
				houderView.linZengSong
						.setBackgroundResource(R.drawable.wodeznegosng2);
			} else if (list.getList().get(position).getSendOut().equals("1")) {
				houderView.linZengSong
						.setBackgroundResource(R.drawable.wodezengsong1);
			}

			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ list.getList().get(position).getHead(),
					houderView.imgRoundImageview);
			return convertView;
		}

	}

	class HouderView {
		private TextView txtShopTitle, txtZengTime, txtMathtoZeng,
				txtmathCountext;
		private TextView txtCIty, txtregion;

		private ImageView imgRoundImageview;

		private LinearLayout linZengSong;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		ShangLaflag = false;
		presenter.ShopZengSong(uid, pageNo + "", "10");
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		ShangLaflag = true;
		pageNo++;
		presenter.ShopZengSong(uid, pageNo + "", "10");
	}

	public void stopLoad() {
		myzengosng_ListView.stopLoadMore();
		myzengosng_ListView.stopRefresh();
	}

}
